package com.g2room.tools.mybatisTool;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.g2room.tools.mybatisTool.to.ClazzTo;

import freemarker.template.Configuration;
import freemarker.template.Template;

public class CreateJdbcRs {
	private static String filePath = "F:/资料/技术资料/开源/Java/搜索/ES/elasticsearchTest/src/main/java/com/gxg/es/index/1";
	public static void main(String[] args) throws IOException {
		List<ClazzTo> clazzTos = TableInfo.getTableInfo();

		for(ClazzTo clazz : clazzTos) {
			System.out.println(clazz.getPoName().toString() + "=" + clazz.getTableName().toString());
		}
		@SuppressWarnings("deprecation")
		Configuration cfg = new Configuration();
		 cfg.setDirectoryForTemplateLoading(new File(CreateFileByFtl.tmplFlord));  
		 createSelect(clazzTos, cfg);
	}
	
	public static void createSelect(List<ClazzTo> clazzTos, Configuration cfg) {
        // 生成配置文件
        try {
            Template tmpl = cfg.getTemplate("SelectMethod.tmpl");
	            StringWriter cfgTmplOut = null;
	            try {
	            	cfgTmplOut = new StringWriter();
	            	Map<String, List<ClazzTo>> clazzToMap = new HashMap<String, List<ClazzTo>>();
	            	clazzToMap.put("clazzs", clazzTos);
	            	tmpl.process(clazzToMap, cfgTmplOut);
	            	// 生成Mybatis Config文件
	            	CreateFileByFtl.createFile(cfgTmplOut.toString(), filePath, "GenericData.java");
	            } catch (Exception e) {
	            	e.printStackTrace();
	            } finally {
					try {
						if(null != cfgTmplOut) cfgTmplOut.close();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
        } catch (IOException e) {
        	e.printStackTrace();
        }
	}
}
